/******************************************************************************/
/* Files to Include                                                           */
/******************************************************************************/

#include <htc.h>           /* Global Header File */
#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */
#include <string.h>

#include "global.h"
#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "display.h"
#include "swI2C.h"
#include "DS1307.h"
#include "tmp75.h"

/******************************************************************************/
/* User Global Variable Declaration                                           */
/******************************************************************************/


uint8_t tempString[50];


/******************************************************************************/
/* Main Program                                                               */

/******************************************************************************/

//__EEPROM_DATA(0x0E, 0x11, 0x0E, 0x12, 0x1F, 0x10, 0x12, 0x19);
//__EEPROM_DATA(0x16,0x11, 0x15, 0x0A, 0x07, 0x04, 0x1F, 0x17,);

uint8_t main(void){
    //local variables
    long minute;
    uint8_t bitTemp, byteTemp;
    uint16_t temp;
    uint8_t count1 = 2;

    /* Configure and define the oscillator frequency */
    ConfigureOscillator();

    /* Initialize I/O and MCU Peripherals */
    InitPeripherals();

    /* Configure external hardware (i.e. -- ic's, sensors, displays)*/
    InitExternalHardware();

    /* Configure MCU timers and interrupts needed*/
    InitTimersAndInterrupts();



    displayString[0] = '\0';
    strcat(displayString, "    rtc I.C. ");
    tempString[0] = ' ';
    tempString[1] = i2cDevices.ds1307;
    tempString[2] = '\0';
    strcat(displayString, tempString);
    DisplayScrollText();

    displayString[0] = '\0';
    strcat(displayString, "    lm75 I.C. ");
    tempString[0] = ' ';
    tempString[1] = i2cDevices.lm75;
    tempString[2] = '\0';
    strcat(displayString, tempString);
    DisplayScrollText();


    /* Main loop*/
    while(1) {

        switch (currentState) {
            case TIME_NUMERICAL:

                displayString[0] = '\0';
                TimeString(tempString);
                strcat(displayString, tempString);
                DisplayTime();

                break;
            case TIME_STRING:
                //RTCGetDateTime();
                displayString[0] = '\0';
                strcat(displayString, "    The time is ");
                TimeString(tempString);
                strcat(displayString, tempString);
                DisplayScrollText();
                break;
            case DATE_TIME_STRING:
                //RTCGetDateTime();
                displayString[0] = '\0';
                strcat(displayString, "    The time is ");
                TimeString(tempString);
                strcat(displayString, tempString);
                strcat(displayString, " on ");
                DateString(tempString);
                strcat(displayString, tempString);
                DisplayScrollText();
                break;
            case TEMP_NUMERICAL:
                displayString[0] = '\0';
                tempString[0]= '\0';
                strcat(displayString, "    RTC Config -> 0b");
                byteTemp = RTCGetControl();
                for (count1 = 0; count1 < 8; count1++) {
                    bitTemp = byteTemp & (1 << (7-count1));
                    if (bitTemp) tempString[count1] = 49;
                    else tempString[count1] = 48;
                }
                strcat(displayString, tempString);
                DisplayScrollText();
                break;


        }
    }

}

/*****************************************************************************
 ** Developement routines
 *****************************************************************************/

/*          //Light leds one by one
 * for(j = 0;j < 8; j++){
 * for (i = 0; i < 16; i++) {
 * DisplayPlot(i, j, ON);
 * DelayMs(20);
 * }
 * }


            // Pulse circle from diameter=1 to diameter=4
 * for (j = 0; j < 5; j++) {
 * for (i = 1; i < 4; i++) {
 * DisplayCircle(8, 4, i, ON);
 * DelayMs(100);
 * ClearDisplay();
 * }
 * for (i = 4; i < 1; i--) {
 * DisplayCircle(8, 4, i, ON);
 * DelayMs(100);
 * ClearDisplay();
 * }
 * }

 *              //Draw 4 small circles(diameter=1) on display
 * DisplayCircle(2,2,1,ON);
 * DelayBigMs(500);
 * DisplayCircle(9,2,1,ON);
 * DelayBigMs(500);
 * DisplayCircle(4,6,1,ON);
 * DelayBigMs(500);
 * DisplayCircle(11,6,1,ON);
 * DelayS(2);
 */
